package Factory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.ImageIcon;

import view.PlantUml.PlantUmlClassDiagrame;
import view.PlantUml.PlantUmlUsecase;
import view.PlantUml.PlantUmlcombination;
import view.interfaces.Iview;

import net.sourceforge.plantuml.SourceStringReader;

import Factory.interfaces.DiagrameFactory;
import Model.diagram.ClassDiagram;
import Model.diagram.CombinationDiagrame;
import Model.diagram.UsecasDiagram;
import Model.interfaces.Idiagrame;

public class UmlFactory extends DiagrameFactory{
	private Iview view;
	public UmlFactory(){
		
	}
	@Override
public	Iview create(Idiagrame dia) {
		
		if(dia instanceof UsecasDiagram){
			view=new PlantUmlUsecase();
			view.addDiagram(dia);
		}
		if(dia instanceof ClassDiagram){
			view=new PlantUmlClassDiagrame();
			view.addDiagram(dia);
		}
		if(dia instanceof CombinationDiagrame){
			view= new PlantUmlcombination();
			view.addDiagram(dia);
		}
		
		
		return view;
	}
	public Iview getView() {
		return view;
	}
	public void setView(Iview view) {
		this.view = view;
	}
	@Override
	public ImageIcon convert(File f) {
		ImageIcon temp =null;
		view.convert();
		try {
			SourceStringReader reader = new SourceStringReader(view.getCode());
			FileOutputStream png =new FileOutputStream(f);
		String desc = reader.generateImage(png);
		temp=new ImageIcon(f.getAbsolutePath());
		png.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return temp;
	}
	

	

}
